package com.payroll.framework;

/*******************************************************
 *                                                     *
 * PAYROLL CONFIDENTIAL AND PROPRIETARY INFORMATION        *
 * DO NOT COPY OR DISLCOSE                             *
 * ALL RIGHTS RESERVED                                 *
 *                                                     *
 *******************************************************
*/
import org.apache.log4j.*;
import java.lang.reflect.*;
import java.util.*;

/**
 * This class holds resources necessary for running the AMS framework. All resources are loaded from the
 * AMSResourceBundle.
 * @author S Popovic
*/
public class PayrollFrameworkProperties {

	private static java.util.Hashtable i_FrameworkProperties;
/**
 * Gets the hastable that contains all the framework properties.
 * @author: S Popovic
 * @return The hashtable of framework properties.
 */
public static Hashtable getFrameworkHash() {
	if(i_FrameworkProperties == null) {
		refresh();
		return i_FrameworkProperties;
	}
	return i_FrameworkProperties;
}
/**
 * Gets the specified property from the hashtable.  All properties in the hashtable are strings.
 * @param s_Key The property key.
 * @return The property value.
 * @author S Popovic
 */
public static String getProperty(String s_Key) {
	return (String) getFrameworkHash().get(s_Key);
}
/**
 * Refreshes the Framework Properties hashtable by clearing the bundle cache and reloading.
 * @author S Popovic
 */
public static void refresh() {
	ResourceBundle frameworkBundle = null;
	Enumeration frameworkEnum = null;
	String frameworkKey = "";
	String frameworkValue = "";

	// Clear the ResourceBundle from cache
	try {
	Class m_class = ResourceBundle.class;
	Field cacheList = m_class.getDeclaredField("cacheList");
	cacheList.setAccessible(true);
	((Map)cacheList.get(ResourceBundle.class)).clear();
	cacheList.setAccessible(false);
	}
	catch(Exception e) {
		Category cat = Category.getInstance("PayrollFrameworkProperties.reloadFrameworkProperties");
		cat.fatal(e.getMessage());
	}


	// load the newly refreshed AMSResourceBundle
	frameworkBundle = ResourceBundle.getBundle("PayrollResourceBundle");
	frameworkEnum = frameworkBundle.getKeys();
	Hashtable props = new Hashtable();

	while(frameworkEnum.hasMoreElements()) {
		frameworkKey = (String) frameworkEnum.nextElement();
		frameworkValue = frameworkBundle.getString(frameworkKey);
		props.put(frameworkKey,frameworkValue);

	}
	i_FrameworkProperties = props;
	}

	public static void setProperty(String p_key, String p_value) {
	getFrameworkHash().put(p_key,p_value);
	}

	public static void removeProperty(String p_key) {
		getFrameworkHash().remove(p_key);
	}
}
